﻿@using Huirui.Cavan.Domain.MainBoundedContext.SystemModule
@model Huirui.Cavan.Presentation.ViewModel.SystemModule.IAccountViewModel
@{Layout = null;}
@using (Html.BeginForm(ActionConst.ADD, ControllerConst.ACCOUNT))
{
    <input id="account_save" type="submit" style="display: none;" />
    <input id="account_reset" type="reset" style="display: none" />

    <div class="tool_bar">
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-save'" onclick="javascript:$('#account_save').click()">保存</a>
        <a href="#" id="accountInitial_edit" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'">编辑</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-copy'" onclick="getChecked()">复制选中项</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-undo'" id="accountReset">重置</a>
        <a href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-collapse'" onclick="toggleHeard()">折叠/展开</a>
        <div id="account_message" class="Horizontal_Message">
        </div>
    </div>
   

    <table class="form_heard">
        <tr>
            <td class="c100">
                <label class="BlueRequired">代码:</label>
            </td>
            <td class="c160">
                @{
    var createTypeList = new List<SelectListItem>();
    createTypeList.Add(new SelectListItem() { Selected = true, Text = "自动生成", Value = "1" });
    createTypeList.Add(new SelectListItem() { Text = "手工输入", Value = "0" });
                }
                <table class="form_heard" style="border-collapse: collapse">
                    <tr>
                        <td class="c80" style="padding: 0">
                            @Html.DropDownList("accountIdCreateType", createTypeList, new { width = "50" })
                        </td>
                        <td class="c80">
                            @Html.TextBoxFor(m => m.NoPrefixId, new { id = "account_NoPrefixId" })
                            @Html.TextBoxFor(m => m.Item.Id, new { id = "account_Id", style = "display: none;" })
                        </td>
                    </tr>
                </table>
            </td>
            <td class="c100">
                <label class="BlueRequired">名称:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Name, new { id = "account_name", @Value = "这里显示账户名称", @class = "withprompt" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label class="BlueRequired">类别:</label>
            </td>
            <td class="c164">
                @Html.DropDownListFor(m => m.Item.Type, typeof(SystemIdentifier.AccountType), new { id = "account_type", @class = "easyui-combobox" })
            </td>
            <td class="c100">
                <label class="BlueRequired">币别:</label>
            </td>
            <td class="c160">
                @Html.TextBoxFor(m => m.Item.Currency, new { id = "account_currency" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label>初始余额:</label>
            </td>
            <td class="c164">
                @Html.TextBoxFor(m => m.Item.InitialAmount, new { id = "account_InitAmount" })
            </td>
            <td class="c100">
                <label>余额日期:</label>
            </td>
            <td class="c164">
                @Html.TextBoxFor(m => m.Item.InitialDate, new { id = "account_InitAmountDate", @class = "Wdate", @Value = (Model.IsNotNull() && Model.Item.IsNotNull()) ? Model.Item.InitialDate.ToString("yyyy-MM-dd") : DateTime.Now.ToString("yyyy-MM-dd"), @type = "text", @onfocus = "WdatePicker({wdate:true,isShowClear:false,readOnly:true,skin:'whyGreen',maxDate:'%y-%M-%d'})" })
            </td>
        </tr>
        <tr>
            <td class="c100">
                <label class="BlueRequired">状态:</label>
            </td>
            <td class="c160">
                <table class="form_heard" style="border-collapse: collapse">
                    <tr>
                        <td class="c80" style="padding: 0">
                            @Html.RadioButtonFor(m => m.Item.Status, SystemIdentifier.AccountStatus.Using.GetHashCode(), new { @id = "account_status1", @checked = "checked" })使用中
                        </td>
                        <td class="c80">
                            @Html.RadioButtonFor(m => m.Item.Status, SystemIdentifier.AccountStatus.Invalid.GetHashCode(), new { @id = "account_status2" })停用
                        </td>
                    </tr>
                </table>
            </td>
            <td class="c100">
            </td>
            <td class="c160">
            </td>
        </tr>
    </table>
}
<table id="account_dg">
</table>
<div id="accounttb">
    <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" id="deleteAccount">选择删除</a>
    <a href="#" class="easyui-linkbutton" iconcls="icon-save" plain="true" onclick="javascript:alert('引出')">引出</a>
</div>
<script type="text/javascript">
    function getChecked()
    {
        var selected = $('#account_dg').datagrid('getChecked');
        if (selected)
        {
            var ids = [];
            for (var i = 0; i < selected.length; i++)
            {
                ids.push(selected[i].Name);
            }
        }
    }

    $(function ()
    {
        $("form[action^='/account/add']").submit(function ()
        {
            CheckEditItem(-1);
            $("#account_NoPrefixId").attr("disabled", false);
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (data)
                {
                    if (data.IsSuccess)
                    {
                        $('#account_dg').datagrid('reload');
                        showLabelMessage('#account_message', '操作成功');
                    }
                },
                error: function ()
                {
                    alert('系统繁忙，请稍候再试');
                }
            })

            return false;
        });

        $('#account_dg').datagrid({
            class: 'easyui-datagrid',
            title: "银行账户",
            idField: 'Id',
            singleSelect: 'true',
            fitColumns: true,
            url: "/account/list",
            toolbar: '#accounttb',
            frozenColumns: [[{ field: 'ck', checkbox: true}]],
            columns: [[
              { field: 'NoPrefixId', title: '账户代码', width: 60 },
              { field: 'Name', title: '名称', width: 100 },
              { field: 'Type', title: '类别', width: 100 },
              { field: 'Currency', title: '币别', width: 100 },
              { field: 'InitialDate', title: '初始日期', width: 100 },
              { field: 'InitialAmount', title: '初始余额', width: 80, align: 'right', editor: 'numberbox' },
              { field: 'Status', title: '状态', width: 100 },
              { field: 'StatusId', hidden: true, width: 0 }
            ]],
            onClickRow: function (rowIndex, rowData)
            {
                $("#account_NoPrefixId").val(rowData.NoPrefixId).attr("disabled", true);
                $("#account_Id").val(rowData.Id);
                $("#account_name").val(rowData.Name);
                $("#account_currency").val(rowData.Currency);
                $("select#account_type").attr('value', rowData.TypeValue);
                $("#account_InitAmount").val(rowData.InitialAmount);
                $("#account_InitAmountDate").val(rowData.InitialDate);
                $("input[name='Item.Status'][value='" + rowData.StatusId + "']").attr('checked', 'true');
                $("input").css('color', '#333333');
                var ed = $('#account_dg').datagrid('getEditor', { index: rowIndex, field: 'InitialAmount' });
                $(ed.target).bind("keyup", function () { $("#account_InitAmount").val($(ed.target).val()); });

            },
            onCheck: function (rowIndex, rowData)
            {
                CheckEditItem(rowIndex);
            },
            onUncheck: function (rowIndex, rowData)
            {
                $('#account_dg').datagrid('cancelEdit', rowIndex);
            }
        });

        $("#deleteAccount").bind("click", function ()
        {
            if ($('#account_dg').datagrid('getChecked').length)
            {
                var selected = $('#account_dg').datagrid('getChecked');

                var effectRow = new Object();
                if (selected.length)
                {
                    effectRow["selected"] = JSON.stringify(selected);
                }

                $.post("/account/delete", effectRow, function (data)
                {
                    if (data.IsSuccess)
                    {
                        $('#account_dg').datagrid('reload');
                        $('#account_dg').datagrid('clearChecked');
                    }
                }, "JSON").error(function ()
                {
                    $.messager.alert("提示", "网络异常，请稍候再试");
                });
            }
        });

        $("#accountReset").bind("click", function ()
        {
            $('#account_reset').click();
            $('#account_dg').datagrid('uncheckAll');
            CheckEditItem(-1);
            if ($('select#accountIdCreateType').val() == "1")
            {
                $("#account_NoPrefixId").attr("disabled", true);
            }
            else
            {
                $("#account_NoPrefixId").attr("disabled", false);
            }
        });

        $("select#accountIdCreateType").bind("change", function ()
        {
            if ($(this).val() == "1")
            {
                $("#account_NoPrefixId").attr("disabled", true);
            }
            else
            {
                $("#account_NoPrefixId").attr("disabled", false);
            }
        });

        function CheckEditItem(exceptIndex)
        {
            var rows = $('#account_dg').datagrid('getRows');
            for (var i = 0; i < rows.length; i++)
            {
                if (exceptIndex < 0)
                {
                    $('#account_dg').datagrid('endEdit', i);
                }
                else if (i != exceptIndex)
                {
                    $('#account_dg').datagrid('cancelEdit', i);
                }
                else
                {
                    $('#account_dg').datagrid('beginEdit', i);
                }
            }
        }

        $('#accountInitial_edit').bind('click', function ()
        {
            var rows = $('#account_dg').datagrid('getRows');
            for (var i = 0; i < rows.length; i++)
            {
                $('#account_dg').datagrid('beginEdit', i);
            }
        })
    });
</script>
